package org.dreamwork.jasmine2.engine;

import org.dreamwork.jasmine2.events.EventException;
import org.dreamwork.jasmine2.web.controls.Label;
import org.dreamwork.jasmine2.web.controls.Literal;
import org.dreamwork.jasmine2.web.controls.Page;
import org.dreamwork.jasmine2.web.controls.Panel;
import org.dreamwork.jasmine2.web.html.ClientScriptControl;
import org.dreamwork.jasmine2.web.html.GenericHtmlControl;

import javax.servlet.http.HttpServletRequest;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.Locale;
import java.util.Map;
import java.util.ResourceBundle;
import java.util.concurrent.ConcurrentHashMap;

/**
 * Created by IntelliJ IDEA.
 * User: seth.yang
 * Date: 2010-9-25
 * Time: 13:51:32
 */
public class DefaultErrorPage extends Page {
    private GenericHtmlControl title, h1, h2, b1, b2, stack_detail, b3, b4;
    private Label b1_text, b2_text, track, version;
    private Panel message;

    private Throwable t;

    private static final Map<Locale, ResourceBundle> pool = new ConcurrentHashMap<Locale, ResourceBundle> ();

    protected DefaultErrorPage (Throwable t) {
        this.t = t;
    }

    private static ResourceBundle getResource (Locale locale) {
        synchronized (pool) {
            ResourceBundle res = pool.get (locale);
            if (res == null) {
                res = ResourceBundle.getBundle ("org.dreamwork.jasmine2.engine.error");
                pool.put (locale, res);
            }
            return res;
        }
    }

    @Override
    public void onPageLoadCompleted (Page page) throws EventException {
        HttpServletRequest request = context.getRequest ();
        Locale locale = request.getLocale ();
        ResourceBundle res = getResource (locale);

        String text = res.getString ("servlet.context.name");
        text = text.replace ("${servlet-context}", request.getContextPath ());
        h1.addControl (new Literal (text));
        b1.addControl (new Literal (res.getString ("desc.title")));
        b1_text.setText (res.getString ("desc.text"));
        b2.addControl (new Literal (res.getString ("desc.detail")));
        message.addControl (new Literal (res.getString ("cause.text")));
        track.setText (res.getString ("stack.title"));
        b3.addControl (new Literal (res.getString ("cause.title")));
        b4.addControl (new Literal (res.getString ("version")));
        version.setText ("2.0");

        if (t != null) {
            String message = t.getMessage ();
            Literal c_message = new Literal (message);
            title.addControl (c_message);
            h2.addControl (c_message);
            b2_text.setText (t.toString () + ":" + message);
            StringWriter sw = new StringWriter ();
            PrintWriter pw = new PrintWriter (sw, true);
            t.printStackTrace (pw);
            sw.flush ();
            stack_detail.addControl (new Literal (sw.getBuffer ()));
        }
    }

    protected void initComponents () {
        this.addControl (new Literal ("\r\n"));
        GenericHtmlControl _auto_genericHtmlControl0 = new GenericHtmlControl ("html");
        _auto_genericHtmlControl0.setId ("_auto_genericHtmlControl0");
        addControl (_auto_genericHtmlControl0);
        html = _auto_genericHtmlControl0;
        GenericHtmlControl _auto_genericHtmlControl1 = new GenericHtmlControl ("head");
        _auto_genericHtmlControl1.setId ("_auto_genericHtmlControl1");
        _auto_genericHtmlControl0.addControl (_auto_genericHtmlControl1);
        head = _auto_genericHtmlControl1;
        GenericHtmlControl _auto_genericHtmlControl2 = new GenericHtmlControl ("META");
        _auto_genericHtmlControl2.setId ("_auto_genericHtmlControl2");
        _auto_genericHtmlControl1.addControl (_auto_genericHtmlControl2);
        _auto_genericHtmlControl2.setAttribute ("CONTENT", "text/html; charset=UTF-8");
        _auto_genericHtmlControl2.setAttribute ("HTTP-EQUIV", "Content-Type");
        title = new GenericHtmlControl ("title");
        title.setId ("title");
        _auto_genericHtmlControl1.addControl (title);
        directControls.add ("title", title);
        title.setRunAtServer (true);
        title.setAttribute ("id", "title");
        GenericHtmlControl _auto_genericHtmlControl3 = new GenericHtmlControl ("style");
        _auto_genericHtmlControl3.setId ("_auto_genericHtmlControl3");
        _auto_genericHtmlControl1.addControl (_auto_genericHtmlControl3);
        _auto_genericHtmlControl3.addControl (new Literal ("* {font-family: \"Verdana\";font-size: 12px;}\r\n        /*p, b {margin-top: -5px}*/\r\n        H1 {font-size: 18px;color: red}\r\n        H2 {font-size: 14px;color: maroon}\r\n        div {background-color:#ffc;border: 1px dashed maroon;margin:5px 0;padding:5px;}\r\n        pre {font-family: \"Courier New\";font-size: 12px;}\r\n        a.handler {text-decoration: none;font-family: \"Courier New\";color: navy;}\r\n        #message {margin-left:10px;}\r\n    "));
        ClientScriptControl _auto_clientScriptControl0 = new ClientScriptControl ();
        _auto_clientScriptControl0.setInnerText ("function ShowOrCloseStack () {\r\n            var Handler = document.getElementById (\"handler\");\r\n            var Stack = document.getElementById (\"stack_info\");\r\n            if (Stack.style.display == 'none') {\r\n                Stack.style.display = 'block';\r\n                Handler.innerHTML = \"[-]\";\r\n            }\r\n            else {\r\n                Stack.style.display = 'none';\r\n                Handler.innerHTML = '[+]';\r\n            }\r\n        }\r\n    ");
        _auto_clientScriptControl0.setId ("_auto_clientScriptControl0");
        _auto_genericHtmlControl1.addControl (_auto_clientScriptControl0);
        directControls.add ("_auto_clientScriptControl0", _auto_clientScriptControl0);
        _auto_clientScriptControl0.setAttribute ("language", "javascript");
        _auto_clientScriptControl0.addControl (new Literal ("function ShowOrCloseStack () {\r\n            var Handler = document.getElementById (\"handler\");\r\n            var Stack = document.getElementById (\"stack_info\");\r\n            if (Stack.style.display == 'none') {\r\n                Stack.style.display = 'block';\r\n                Handler.innerHTML = \"[-]\";\r\n            }\r\n            else {\r\n                Stack.style.display = 'none';\r\n                Handler.innerHTML = '[+]';\r\n            }\r\n        }\r\n    "));
        GenericHtmlControl _auto_genericHtmlControl4 = new GenericHtmlControl ("body");
        _auto_genericHtmlControl4.setId ("_auto_genericHtmlControl4");
        _auto_genericHtmlControl0.addControl (_auto_genericHtmlControl4);
        _auto_genericHtmlControl4.setAttribute ("bgcolor", "white");
        body = _auto_genericHtmlControl4;
        h1 = new GenericHtmlControl ("H1");
        h1.setId ("h1");
        _auto_genericHtmlControl4.addControl (h1);
        directControls.add ("h1", h1);
        h1.setRunAtServer (true);
        h1.setAttribute ("id", "h1");
        GenericHtmlControl _auto_genericHtmlControl5 = new GenericHtmlControl ("hr");
        _auto_genericHtmlControl5.setId ("_auto_genericHtmlControl5");
        _auto_genericHtmlControl4.addControl (_auto_genericHtmlControl5);
        _auto_genericHtmlControl5.setAttribute ("color", "silver");
        _auto_genericHtmlControl5.setAttribute ("width", "100%");
        _auto_genericHtmlControl5.setAttribute ("size", "1");
        h2 = new GenericHtmlControl ("h2");
        h2.setId ("h2");
        _auto_genericHtmlControl4.addControl (h2);
        directControls.add ("h2", h2);
        h2.setRunAtServer (true);
        h2.setAttribute ("id", "h2");
        GenericHtmlControl _auto_genericHtmlControl6 = new GenericHtmlControl ("p");
        _auto_genericHtmlControl6.setId ("_auto_genericHtmlControl6");
        _auto_genericHtmlControl4.addControl (_auto_genericHtmlControl6);
        b1 = new GenericHtmlControl ("b");
        b1.setId ("b1");
        _auto_genericHtmlControl6.addControl (b1);
        directControls.add ("b1", b1);
        b1.setRunAtServer (true);
        b1.setAttribute ("id", "b1");
        b1_text = new org.dreamwork.jasmine2.web.controls.Label ();
        b1_text.setId ("b1_text");
        _auto_genericHtmlControl6.addControl (b1_text);
        directControls.add ("b1_text", b1_text);
        b1_text.setAttribute ("id", "b1_text");
        GenericHtmlControl _auto_genericHtmlControl7 = new GenericHtmlControl ("p");
        _auto_genericHtmlControl7.setId ("_auto_genericHtmlControl7");
        _auto_genericHtmlControl4.addControl (_auto_genericHtmlControl7);
        b2 = new GenericHtmlControl ("b");
        b2.setId ("b2");
        _auto_genericHtmlControl7.addControl (b2);
        directControls.add ("b2", b2);
        b2.setRunAtServer (true);
        b2.setAttribute ("id", "b2");
        b2_text = new org.dreamwork.jasmine2.web.controls.Label ();
        b2_text.setId ("b2_text");
        _auto_genericHtmlControl7.addControl (b2_text);
        directControls.add ("b2_text", b2_text);
        b2_text.setAttribute ("id", "b2_text");
        b3 = new GenericHtmlControl ("b");
        b3.setId ("b3");
        _auto_genericHtmlControl4.addControl (b3);
        directControls.add ("b3", b3);
        b3.setRunAtServer (true);
        b3.setAttribute ("id", "b3");
        message = new org.dreamwork.jasmine2.web.controls.Panel ();
        message.setId ("message");
        _auto_genericHtmlControl4.addControl (message);
        directControls.add ("message", message);
        message.setAttribute ("id", "message");
        GenericHtmlControl _auto_genericHtmlControl8 = new GenericHtmlControl ("p");
        _auto_genericHtmlControl8.setId ("_auto_genericHtmlControl8");
        _auto_genericHtmlControl4.addControl (_auto_genericHtmlControl8);
        GenericHtmlControl _auto_genericHtmlControl9 = new GenericHtmlControl ("b");
        _auto_genericHtmlControl9.setId ("_auto_genericHtmlControl9");
        _auto_genericHtmlControl8.addControl (_auto_genericHtmlControl9);
        GenericHtmlControl handler = new GenericHtmlControl ("a");
        handler.setId ("handler");
        _auto_genericHtmlControl9.addControl (handler);
        directControls.add ("handler", handler);
        handler.setAttribute ("href", "#");
        handler.setAttribute ("class", "handler");
        handler.setAttribute ("onclick", "ShowOrCloseStack ()");
        handler.setAttribute ("id", "handler");
        handler.addControl (new Literal ("[+]"));
        track = new org.dreamwork.jasmine2.web.controls.Label ();
        track.setId ("track");
        _auto_genericHtmlControl9.addControl (track);
        directControls.add ("track", track);
        track.setAttribute ("id", "track");
        GenericHtmlControl stack_info = new GenericHtmlControl ("div");
        stack_info.setId ("stack_info");
        _auto_genericHtmlControl4.addControl (stack_info);
        directControls.add ("stack_info", stack_info);
        stack_info.setAttribute ("style", "display:none");
        stack_info.setAttribute ("id", "stack_info");
        stack_detail = new GenericHtmlControl ("pre");
        stack_detail.setId ("stack_detail");
        stack_info.addControl (stack_detail);
        directControls.add ("stack_detail", stack_detail);
        stack_detail.setRunAtServer (true);
        stack_detail.setAttribute ("id", "stack_detail");
        GenericHtmlControl _auto_genericHtmlControl10 = new GenericHtmlControl ("hr");
        _auto_genericHtmlControl10.setId ("_auto_genericHtmlControl10");
        _auto_genericHtmlControl4.addControl (_auto_genericHtmlControl10);
        _auto_genericHtmlControl10.setAttribute ("color", "silver");
        _auto_genericHtmlControl10.setAttribute ("width", "100%");
        _auto_genericHtmlControl10.setAttribute ("size", "1");
        b4 = new GenericHtmlControl ("b");
        b4.setId ("b4");
        _auto_genericHtmlControl4.addControl (b4);
        directControls.add ("b4", b4);
        b4.setRunAtServer (true);
        b4.setAttribute ("id", "b4");
        _auto_genericHtmlControl4.addControl (new Literal (": Jasmine "));
        version = new org.dreamwork.jasmine2.web.controls.Label ();
        version.setId ("version");
        _auto_genericHtmlControl4.addControl (version);
        directControls.add ("version", version);
        version.setAttribute ("id", "version");
    }

    public String getUniqueId () {
        return "jasmine.autogen.error_jasmine";
    }
}